import axios from 'axios';

const api = axios.create({
    baseURL: 'http://localhost:8082', // 后端API地址
    timeout: 5000
});

// 获取所有部门列表
export const getAllDepartments = async () => {
    try {
        const response = await api.get('/department/queryAllDeptInfo');
        return response.data;
    } catch (error) {
        console.error('获取部门列表失败:', error);
        throw error;
    }
};

// 根据ID获取部门信息
export const getDepartmentById = async (departmentId) => {
    try {
        const response = await api.get(`/department/queryDeptInfoById/${departmentId}`);
        return response.data;
    } catch (error) {
        console.error('获取部门信息失败:', error);
        throw error;
    }
};

// 检查部门号是否唯一
export const checkDepartmentCodeUnique = async (departmentCode) => {
    try {
        const response = await api.get(`/department/checkDeptCodeUnique/${departmentCode}`);
        return response.data;
    } catch (error) {
        console.error('检查部门名称唯一性失败:', error);
        throw error;
    }
};

// 检查部门名称是否唯一
export const checkDepartmentNameUnique = async (departmentName) => {
    try {
        const response = await api.get(`/department/checkDeptNameUnique/${departmentName}`);
        return response.data;
    } catch (error) {
        console.error('检查部门名称唯一性失败:', error);
        throw error;
    }
};

/**
 * 添加部门信息
 * @param {String} departmentCode - 部门编号
 * @param {String} departmentName - 部门名称
 * @param {String} remarks - 备注信息
 * @author 吴文钦
 * @returns {Promise<Result>} - 返回添加结果的 Result 对象
 */
export const addDepartment = async (departmentCode, departmentName, remarks) => {
    try {
        const response = await api.post(`/department/insertDeptInfo/${departmentCode}/${departmentName}/${remarks}`);
        return response.data;
    } catch (error) {
        console.error('添加部门失败:', error);
        throw error;
    }
};

// 更新部门信息
export const updateDepartment = async (departmentData) => {
    try {
        const response = await api.put('/department/updateDeptInfo', departmentData);
        return response.data;
    } catch (error) {
        console.error('更新部门失败:', error);
        throw error;
    }
};



export default {
    getAllDepartments,
    getDepartmentById,
    checkDepartmentNameUnique,
    addDepartment,
    updateDepartment,
};